<?php
class Follow extends Controller{
	
	var $name = 'follow';
	
	/**
	 * 
	 * Enter description here ...
	 */
	public function index_action(){
		
		$this->bar_title = 'Acompanhar meus alunos';
		
		$data['students'] = $this->student_users;

		$this->view('index', $data);		
	}
	
	/**
	 * 
	 * Enter description here ...
	 */
	public function units() {
		$courseid = $this->course->id;
		$userid = $this->getParam('student');
		
		if($this->student_users[$userid] == null) {
			$this->setErro("Erro. Usuário não existe.");
			header('Location: '.WWW_APP.'follow/');
		}
		
		$this->bar_title = "Acompanhar aluno: ".$this->student_users[$userid]->firstname;
		
		$units = $this->getUnits($courseid);
		$data['units'] = $units;
		$data['userid'] = $userid;
		
		$this->view('units', $data);
	}
	
	public function log() {
		$sumConcepts = array();
		$conceptsCorrect = array();
		$unitid = $this->getParam('unit');
		$userid = $this->getParam('student');
		//Nome da unidade
		$nameUnit = $this->DB->get_record('oaeditor_units', array('id'=>$unitid));
		$nameUnit = $nameUnit->path;
		
		$this->bar_title = "Acompanhar aluno: ".$this->student_users[$userid]->firstname;
				
		$allConcepts = $this->getAllConcepts($nameUnit);
		$exams = $this->getExams($userid, $unitid);
		$tree = $this->tree_vision($unitid, $userid);
		
		
		foreach($exams as $exam) {
			$questions = $this->getQuestions($exam->id);
			foreach($questions as $question) {
				$sumConcepts[$question->related_concept] += 1;
				if($question->answer_correct == $question->marked) {
					$conceptsCorrect[$question->related_concept] += 1;
				}else {
					$conceptsIncorrect[$question->related_concept] += 1;
				}
			}
		}
		//
		ksort($allConcepts);
		ksort($sumConcepts);
		ksort($conceptsCorrect);
		ksort($conceptsIncorrect);
		//
		$data['unitid'] = $unitid;
		$data['student'] = $userid;
		$data['allConcepts'] = $allConcepts;
		$data['sumConcepts'] = $sumConcepts;
		$data['conceptsCorrect'] = $conceptsCorrect;
		$data['conceptsIncorrect'] = $conceptsIncorrect;
		$data['treeview'] = $tree;
		
		$this->view('log', $data);
	}
	
	/**
	 * 
	 * Enter description here ...
	 */
	public function exams() {
		$userid = $this->getParam('student');
		$unitid = $this->getParam('unit');
		$this->bar_title = "Acompanhar aluno: ".$this->student_users[$userid]->firstname;
				
		$allExams = $this->getExams($userid, $unitid);
		$tree = $this->tree_vision($unitid, $userid);
		$data['exams'] = $allExams;
		$data['student'] = $userid;
		$data['unitid'] = $unitid;
		$data['treeview'] = $tree;
		
		$this->view('exams', $data);
	}
	
	/**
	 * 
	 * Enter description here ...
	 */
	public function questions() {
		
		$userid = $this->getParam('student');
		$examid = $this->getParam('exam');
		$unitid = $this->getParam('unit');	
		
		$this->bar_title = "Acompanhar aluno: ".$this->student_users[$userid]->firstname;
					
		$allQuestions = $this->getQuestions($examid);
		if(empty($allQuestions)) {
			$this->setErro("Erro ao tentar recuperar as questões da unidade.");
			header('Location: '.WWW_APP.'follow/');
		}
		$data['allQuestions'] = $allQuestions;
		$note = $this->getNote($examid);
		$data['note'] = $note;
		$data['unitid'] = $unitid;
		$data['student'] = $userid;
		
		$this->view('questions', $data);		
	}
		
	/**
	 * 
	 * Enter description here ...
	 * @param $courseid
	 */
	private function getUnits($courseid) {
		$conditions = array('courseid'=>$courseid);
		$units = $this->DB->get_records('oaeditor_units', $conditions);
		
		return $units;		
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $userid
	 * @param unknown_type $unitid
	 */
	private function getExams($userid, $unitid) {		
		
		$conditions = array('userid'=>$userid, 'unitid'=>$unitid);
		$allExams = $this->DB->get_records('oaeditor_exams', $conditions);

		return $allExams;
	}
	
/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $examid
	 */
	private function getQuestions($examid) {
		//
		$questions = array();
		//
		$conditionExam = array('examid'=>$examid);
		$allQuestions = $this->DB->get_records('oaeditor_questions_resolved', $conditionExam);
		//
		foreach($allQuestions as $value) {
			$conditionQuestion = array('id'=>$value->questionid);
			$question = $this->DB->get_record('oaeditor_questions', $conditionQuestion);
			$question->marked = $value->marked;			
			$questions[] = $question;
		}
		
		return $questions;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $examid
	 */
	private function getNote($examid) {
		//
		$condition = array('id'=>$examid);
		$exam = $this->DB->get_record('oaeditor_exams', $condition);
		
		return $exam->note;		
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $id_unit
	 */
	private function tree_vision($id_unit, $id_user){
		
		$unit = $this->DB->get_record('oaeditor_units', array('id'=>$id_unit));
		$name_unit = $unit->path;
		
		$xml = simplexml_load_file(FILES.$name_unit.DS."graph.xml");
    
    	$pilha = array('0'); //Inicia a pilha com primeiro elemento com valor 0

    	$conditions = array('userid'=>$id_user, 'unitid'=>$id_unit, 'conceptid'=>'0');
    	$count = $this->DB->count_records('oaeditor_logs', $conditions);
    	$view = '<ul id="browser" class="filetree"><li><span class="folder">'.$name_unit.' ( '. ($count) .' )</span><ul>';
    	 
    	while (!empty($pilha)){
    
    		//Seta filho como 0
    		$son = 0;
    
    		foreach ($xml->edge as $edge){
    			//Verifica se o topo da pilha tem filho
    			if( $edge['source'] == $pilha[count($pilha)-1] && $edge['ref'] == 'false'){
    				$son = (int) $edge['target'];
    				unset($edge[0]);
    				break;
    			}
    		}
    
    		//Verifica se encontrou o filho
    		if($son != 0){
    			 
    			foreach ($xml->node as $node){
    				if( $node['id'] == $son ){
    					$conditions = array('userid'=>$id_user, 'unitid'=>$id_unit, 'conceptid'=>$node['id']);
    					$count = $this->DB->count_records('oaeditor_logs', $conditions);
    					$view .= '<li><span class="file">'.$node['name'].' ( '. $count .' )</span><ul>';
    					break;
    				}
    			}
    			array_push($pilha, $son);
    			 
    		}else{
    			$view .= '</ul></li>';
    			array_pop($pilha);
    		}    
    	}
    	
    	return str_replace('<ul></ul>', '', $view);
    }	
    
	/**
	 * 
	 * Método que retorna um array de conceitos da unidade passada como parâmetro
	 * @param String $name_unit   nome da unidade
	 */
	private function getAllConcepts($name_unit) {
		
		//Lê o xml da unidade
		$xml = simplexml_load_file(FILES.$name_unit.DS."graph.xml");
		
		if(!$xml) {
			return false;
		}
		$nodes[0] = "Introdução";
		//Monta o array de conceitos
		foreach($xml->node as $node) {
    		$nodes[(String)$node['id']] = (String)$node['name'];			
    	}
    	
    	//retorna o array de conceitos
   		return $nodes;
   				
	}
}